var EnterpriseAdd = function() {

	var handleValidation = function() {
		var _url = window.location.pathname;
		var addFlag = _url.indexOf("add") != -1 ? true : false;

		var formAdd = $('#addForm');

		var validator = formAdd.validate({
			rules : {
				name : {
					required : true,
					rangelength : [1, 64 ],
					remote: {
                        url: ctx + "/enterprise/check",
                        type: "post",
                        dataType: "json",
                        data: {name : $("#enterpriseName").value}
                    }
				},
				createTime : {
					required : true
				},
				legalPerson : {
					required : true,
					rangelength : [1, 50 ]
				},
				businessRegistrationNo : {
					required : true,
					rangelength : [ 1, 64 ]
				},
				address : {
					required : true,
					rangelength : [ 1, 64 ]
				},
				contacts : {
					required : false,
					rangelength : [1, 12 ]
				},
				mobile : {
					required : true,
					number : true,
					rangelength : [ 11, 11 ]
				}
			},
            messages: {
            	mobile: {rangelength:"请输入正确长度的手机号码"},
            	name: {remote:"企业名称已被使用！"},
            	productName: {remote:"产品全称已被使用！"},
            	briefName: {remote:"产品简称已被使用！"},
            	openDate:{ required:"开放式，此字段必填！"},
            	duration:{digits:"只能输入非负整数"},
            	scale:{min:"只能输入大于等于0的数"},
            	subStart:{min:"只能输入大于等于0的数"},
            	increaseAmount:{min:"只能输入大于等于0的数"}
            },
			success : function(label, element) {
				var icon = $(element).parent('.input-icon').children('i');
				$(element).closest('.form-group').removeClass('has-error')
						.addClass('has-success'); // set success class to the
			},
			submitHandler : function(form) {
			}
		});
		
		
		var shareholderForm = $('#shareholderForm');

		var shareholdervalidator = shareholderForm.validate({
			rules : {
				name : {
					required : true,
					rangelength : [ 1, 20 ],
				},
				position : {
					required : true,
					rangelength:[1,20]
				},
				totalShares : {
					required : true,
					number : true,
					range:[0,100]
				},
				actualContributiveAmount : {
					required : true,
					number : true,
				},
				introduction : {
					required : true
				}
			},
            messages: {
            },
			success : function(label, element) {
				var icon = $(element).parent('.input-icon').children('i');
				$(element).closest('.form-group').removeClass('has-error')
						.addClass('has-success'); // set success class to the
			},
			submitHandler : function(form) {
				e.preventDefault();
				return;
			}
		});
		
		var ReportForm = $('#ReportForm');

		var Reportvalidator = ReportForm.validate({
			rules : {
				createTime : {
					required : true,
					dateISO:true,
				},
				reportCompany : {
					required : true,
					rangelength:[1,30]
				}
			},
            messages: {
            },
			success : function(label, element) {
				var icon = $(element).parent('.input-icon').children('i');
				$(element).closest('.form-group').removeClass('has-error')
						.addClass('has-success'); // set success class to the
			},
			submitHandler : function(form) {
				e.preventDefault();
				return;
			}
		});
		
	}

	return {
		init : function() {
			handleValidation();
		}
	};
}();



$("#addReportBtn").click(function(){
	 $('#ReportForm').reset(); 
	 $('#saveReportMiss').click(); 
});

function addReportReset(){
	 $('#saveReportMiss').click(); 
}

//财务报表数据数组
var WholeReportData =  new Array();
//当前操作行数
var currentLine;
//当前操作财务报表id数组
var currentReport = new Array();
//当前操作财务报表编制日数组
var currentReportDate = new Array();

$("#saveReport").click(function(){
	var shareholderForm = $('#ReportForm');
	var flag = shareholderForm.valid();
    if(!flag){
    	return flag;
    }
    var length = WholeReportData.length;
    var reportTime = $("#report_time").val();
    var reportCompany = $("#report_company").val();
	$("#reportTable tr:eq(0)").after("<tr role=\"row\" class=\"odd\"><td> <center>"+$("#report_time").val()+"</center></td>"+
			"<td>"+
			"<center>"+$("#report_company").val()+"</center>"+
			"</td>"+
			"<td>"+
	        "<center> <button type=\"button\" onclick=\"show_assetLiabilityModel("+length+")\";  class=\"btn btn-white\">资产负债表</button><center>"+
	        "</td>"+
			"<td>"+
			"<center><button type=\"button\"  onclick=\"show_profitModel("+length+")\"; class=\"btn btn-white\">利润表</button></center>"+
			"</td>"+
			"<td>"+
	        "<center><button type=\"button\" onclick=\"show_cashModel("+length+")\"; class=\"btn btn-white\">现金流量表</button><center>"+
	        "</td>"+
	        "<td>"+
            "<center><button type=\"button\"  disabled onclick=\"show_financeModel("+length+")\"; class=\"btn btn-white\">主要财务指标</button><center>"+
            "</td>"+
	"</tr>");
	$("#saveReportMiss").click();
	$("#saveReportReset").click();
	addNewReportData(reportTime,reportCompany);
});


function show_assetLiabilityModel(t){
	currentLine = t;
	$("#tabulation_date").html(currentReportDate[t]);
	//清空model中的数据
	for(var i=1;i<45;i++){
		$("#alb_start_"+i).val("");
		$("#alb_end_"+i).val("");
	}
	//填充内存中的数据
	for(var i=1;i<45;i++){
		$("#alb_start_"+i).val(WholeReportData[t][1][i-1]);
		$("#alb_end_"+i).val(WholeReportData[t][2][i-1]);
	}
	$("#assetLiability").click();
}

function show_profitModel(t){
	currentLine = t;
	$("#profit_date").html(currentReportDate[t]);
	//清空model中的数据
	for(var i=1;i<16;i++){
		$("#pft_start_"+i).val("");
		$("#pft_end_"+i).val("");
	}
	//填充内存中的数据
	for(var i=1;i<16;i++){
		$("#pft_start_"+i).val(WholeReportData[t][3][i-1]);
		$("#pft_end_"+i).val(WholeReportData[t][4][i-1]);
	}
	$("#profit").click();
}

function show_cashModel(t){
	currentLine = t;
	$("#cash_date").html(currentReportDate[t]);
	//清空model中的数据
	for(var i=1;i<22;i++){
		$("#cash_start_"+i).val("");
		$("#cash_end_"+i).val("");
	}
	//填充内存中的数据
	for(var i=1;i<22;i++){
		$("#cash_start_"+i).val(WholeReportData[t][5][i-1]);
		$("#cash_end_"+i).val(WholeReportData[t][6][i-1]);
	}
	$("#cash").click();
}

function show_financeModel(t){
	currentLine = t;
	$("#finance_date").html(currentReportDate[t]);
	//清空model中的数据
	for(var i=1;i<23;i++){
		$("#fin_start_"+i).val("");
		$("#fin_end_"+i).val("");
	}
	//填充内存中的数据
	for(var i=1;i<22;i++){
		$("#fin_start_"+i).val(WholeReportData[t][7][i-1]);
		$("#fin_end_"+i).val(WholeReportData[t][8][i-1]);
	}
	$("#finance").click();
}


//保存资产负债表
$("#saveAssetLiability").click(function(){
	//先进行一次合计计算
	assetLiabilityDataChange();
	//保存数据
	for(var i=1;i<45;i++){
		WholeReportData[currentLine][1][i-1] = $("#alb_start_"+i).val();
		WholeReportData[currentLine][2][i-1] = $("#alb_end_"+i).val();
	}
	$("#saveAssetLiabilityMiss").click();
	//保存资产负债表数据
	var json = $('form#assetLiabilityForm').serializeJSON();
	var js = json.replace('}', ',');
 	json = js + "\"financialReportId\":\""+currentReport[currentLine]+"\"}";
	$.ajax({  
	     type:"post",
        contentType:"application/json; charset=UTF-8",
        url: ctx + "/enterprise/saveAssetLiability",
        data:json,
        dataType:"json",
	     success:function(data) {  
	    	 if(data.code == 1){
	    		 bootbox.alert("保存资产负债数据成功！");
	    	 }
	    	 return false;
	      },  
	      error : function() {  
	    	  bootbox.alert("保存资产负债数据失败！");
	    	  return false;
	      }  
	 });
});
//保存利润表
$("#saveProfit").click(function(){
	//先进行一次合计计算
	profitDataChange();
	//保存数据
	for(var i=0;i<16;i++){
		WholeReportData[currentLine][1][i-1] = $("#pft_start_"+i).val();
		WholeReportData[currentLine][2][i-1] = $("#pft_end_"+i).val();
	}
	$("#saveProfitMiss").click();
	//保存利润表数据
	var json = $('form#profitForm').serializeJSON();
	var js = json.replace('}', ',');
 	json = js + "\"financialReportId\":\""+currentReport[currentLine]+"\"}";
	$.ajax({  
	     type:"post",
        contentType:"application/json; charset=UTF-8",
        url: ctx + "/enterprise/saveProfit",
        data:json,
        dataType:"json",
	     success:function(data) {  
	    	 if(data.code == 1){
	    		 bootbox.alert("保存利润表数据成功！");
	    	 }
	    	 return false;
	      },  
	      error : function() {  
	    	  bootbox.alert("保存利润表数据失败！");
	    	  return false;
	      }  
	 });
});
//保存现金流量
$("#saveCash").click(function(){
	//先进行一次合计计算
	cashDataChange();
	//保存数据
	for(var i=1;i<45;i++){
		WholeReportData[currentLine][1][i-1] = $("#cash_start_"+i).val();
		WholeReportData[currentLine][2][i-1] = $("#cash_end_"+i).val();
	}
	$("#saveCashMiss").click();
	//保存现金流量表数据
	var json = $('form#cashForm').serializeJSON();
	var js = json.replace('}', ',');
 	json = js + "\"financialReportId\":\""+currentReport[currentLine]+"\"}";
	$.ajax({  
	     type:"post",
        contentType:"application/json; charset=UTF-8",
        url: ctx + "/enterprise/saveCash",
        data:json,
        dataType:"json",
	     success:function(data) {  
	    	 if(data.code == 1){
	    		 bootbox.alert("保存现金流量表数据成功！");
	    	 }
	    	 return false;
	      },  
	      error : function() {  
	    	  bootbox.alert("保存现金流量表数据失败！");
	    	  return false;
	      }  
	 });
});
//保存财务指标
$("#saveFinance").click(function(){
	//先进行一次合计计算
	//保存数据
	for(var i=1;i<45;i++){
		WholeReportData[currentLine][1][i-1] = $("#fin_start_"+i).val();
		WholeReportData[currentLine][2][i-1] = $("#fin_end_"+i).val();
	}
	$("#saveFinanceMiss").click();
	//保存财务指标数据
	var json = $('form#financeForm').serializeJSON();
	var js = json.replace('}', ',');
 	json = js + "\"financialReportId\":\""+currentReport[currentLine]+"\"}";
	$.ajax({  
	     type:"post",
        contentType:"application/json; charset=UTF-8",
        url: ctx + "/enterprise/saveFinance",
        data:json,
        dataType:"json",
	     success:function(data) {  
	    	 if(data.code == 1){
	    		 bootbox.alert("保存财务指标数据成功！");
	    	 }
	    	 return false;
	      },  
	      error : function() {  
	    	  bootbox.alert("保存财务指标数据失败！");
	    	  return false;
	      }  
	 });
});


//新增一条财务报表数据
function addNewReportData(time,company){
	//资产负债
	var assetLiabilityStart = new Array(); 
	var assetLiabilityEnd = new Array(); 
	for(var i=0;i<44;i++){
		assetLiabilityStart[i] = 0;
		assetLiabilityEnd[i] = 0;
	}
	//利润表
	var profitStart = new Array();
	var profitEnd = new Array(); 
	for(var i=0;i<15;i++){
		profitStart[i] = 0;
		profitEnd[i] = 0;
	}
	//现金流量
	var cashStart = new Array();
	var cashEnd = new Array(); 
	for(var i=0;i<21;i++){
		cashStart[i] = 0;
		cashEnd[i] = 0;
	}
	//主要财务指标
    var financeStart = new Array();
    var financeEnd = new Array(); 
    for(var i=0;i<22;i++){
    	financeStart[i] = 0;
    	financeEnd[i] = 0;
	}
    var length = WholeReportData.length;
    WholeReportData[length] = new Array();
    WholeReportData[length][1] = assetLiabilityStart;
    WholeReportData[length][2] = assetLiabilityEnd;
    WholeReportData[length][3] = profitStart;
    WholeReportData[length][4] = profitEnd;
    WholeReportData[length][5] = cashStart;
    WholeReportData[length][6] = cashEnd;
    WholeReportData[length][7] = financeStart;
    WholeReportData[length][8] = financeEnd;
    currentReportDate[length] = time;
    //新增一条空的财务报表数据
    var json ="{\"enterpriseId\":\""+$("#enterpriseId").val()+"\",\"reportTime\":\""+time+"\",\"reportCompany\":\""+company+"\"}";
	$.ajax({  
	     type:"post",
         contentType:"application/json; charset=UTF-8",
         url: ctx + "/enterprise/addFinancialReport",
         data:json,
         dataType:"json",
	     success:function(data) {  
	    	 if(data.code == 1){
	    		 currentReport[length] = data.data;
	    		 return true;
	    	 }
	    	 return false;
	      },  
	      error : function() {  
	    	  bootbox.alert("新增财务报表失败！");
	    	  return false;
	      }  
	 });
}

//资产负债表输入改变时进行合计计算
function assetLiabilityDataChange(){
	var stats_start_1=0;
	var stats_end_1=0;
	for(var i=1;i<11;i++){
		 stats_start_1 =stats_start_1+Number($("#alb_start_"+i).val());
		 stats_end_1 =stats_end_1+Number($("#alb_end_"+i).val());
	}
	$("#alb_start_11").val(stats_start_1);
	$("#alb_end_11").val(stats_end_1);
	
	$("#alb_start_16").val(Number($("#alb_start_14").val())-Number($("#alb_start_15").val()));
	$("#alb_end_16").val(Number($("#alb_end_14").val())-Number($("#alb_end_15").val()));
	
	
	var stats_start_3=0;
	var stats_end_3=0;
	for(var j=12;j<22;j++){
		if(j != 14 && j != 15){
			stats_start_3 =stats_start_3+Number($("#alb_start_"+j).val());
			stats_end_3 =stats_end_3+Number($("#alb_end_"+j).val());
		}
	}
	$("#alb_start_22").val(stats_start_3);
	$("#alb_end_22").val(stats_end_3);
	
	$("#alb_start_23").val(Number($("#alb_start_11").val())+stats_start_3);
	$("#alb_end_23").val(Number($("#alb_end_11").val())+stats_end_3);
	
	var stats_start_4=0;
	var stats_end_4=0;
	for(var m=24;m<33;m++){
			stats_start_4 =stats_start_4+Number($("#alb_start_"+m).val());
			stats_end_4 =stats_end_4+Number($("#alb_end_"+m).val());
	}
	$("#alb_start_33").val(stats_start_4);
	$("#alb_end_33").val(stats_end_4);
	
	$("#alb_start_37").val(Number($("#alb_start_34").val())+Number($("#alb_start_35").val())+Number($("#alb_start_36").val()));
	$("#alb_end_37").val(Number($("#alb_end_34").val())+Number($("#alb_end_35").val())+Number($("#alb_end_36").val()));
	
	$("#alb_start_38").val(Number($("#alb_start_33").val())+Number($("#alb_start_37").val()));
	$("#alb_end_38").val(Number($("#alb_end_33").val())+Number($("#alb_end_37").val()));
	
	$("#alb_start_43").val(Number($("#alb_start_39").val())+Number($("#alb_start_40").val())+Number($("#alb_start_41").val())+Number($("#alb_start_42").val()));
	$("#alb_end_43").val(Number($("#alb_end_39").val())+Number($("#alb_end_40").val())+Number($("#alb_end_41").val())+Number($("#alb_end_42").val()));
	
	$("#alb_start_44").val(Number($("#alb_start_23").val()));
	$("#alb_end_44").val(Number($("#alb_end_23").val()));
}

//利润表输入改变时进行合计计算
function profitDataChange(){
	$("#pft_start_4").val(Number($("#pft_start_1").val())-Number($("#pft_start_2").val())-Number($("#pft_start_3").val()));
	$("#pft_end_4").val(Number($("#pft_end_1").val())-Number($("#pft_end_2").val())-Number($("#pft_end_3").val()));
	
	$("#pft_start_11").val(Number($("#pft_start_4").val())+Number($("#pft_start_5").val())+Number($("#pft_start_6").val())-Number($("#pft_start_7").val())-Number($("#pft_start_8").val())-Number($("#pft_start_9").val())-Number($("#pft_start_10").val()));
	$("#pft_end_11").val(Number($("#pft_end_4").val())+Number($("#pft_end_5").val())+Number($("#pft_end_6").val())-Number($("#pft_end_7").val())-Number($("#pft_end_8").val())-Number($("#pft_end_9").val())-Number($("#pft_start_10").val()));
	
	$("#pft_start_13").val(Number($("#pft_start_11").val())+Number($("#pft_start_12").val()));
	$("#pft_end_13").val(Number($("#pft_end_11").val())+Number($("#pft_end_12").val()));
	
	$("#pft_start_15").val(Number($("#pft_start_13").val())-Number($("#pft_start_14").val()));
	$("#pft_end_15").val(Number($("#pft_end_13").val())-Number($("#pft_end_14").val()));
}

//现金流量表改变时进行合计计算
function cashDataChange(){
	$("#cash_start_7").val(Number($("#cash_start_1").val())+Number($("#cash_start_2").val())-Number($("#cash_start_3").val())-Number($("#cash_start_4").val())-Number($("#cash_start_5").val())-Number($("#cash_start_6").val()));
	$("#cash_end_7").val(Number($("#cash_end_1").val())+Number($("#cash_end_2").val())-Number($("#cash_end_3").val())-Number($("#cash_end_4").val())-Number($("#cash_end_5").val())-Number($("#cash_end_6").val()));
	
	$("#cash_start_13").val(Number($("#cash_start_8").val())+Number($("#cash_start_9").val())+Number($("#cash_start_10").val())-Number($("#cash_start_11").val())-Number($("#cash_start_12").val()));
	$("#cash_end_13").val(Number($("#cash_end_8").val())+Number($("#cash_end_9").val())+Number($("#cash_end_10").val())-Number($("#cash_end_11").val())-Number($("#cash_end_12").val()));
	
	$("#cash_start_18").val(Number($("#cash_start_14").val())+Number($("#cash_start_15").val())-Number($("#cash_start_16").val())-Number($("#cash_start_17").val()));
	$("#cash_end_18").val(Number($("#cash_end_14").val())+Number($("#cash_end_15").val())-Number($("#cash_end_16").val())-Number($("#cash_end_17").val()));
	
	$("#cash_start_19").val(Number($("#cash_start_7").val())+Number($("#cash_start_13").val())+Number($("#cash_start_18").val()));
	$("#cash_end_19").val(Number($("#cash_end_7").val())+Number($("#cash_end_13").val())+Number($("#cash_end_18").val()));
	
	$("#cash_start_21").val(Number($("#cash_start_19").val())+Number($("#cash_start_20").val()));
	$("#cash_end_21").val(Number($("#cash_end_19").val())+Number($("#cash_end_20").val()));
	
}

//////////////////////////////////////////////////
$("#addShareholderBtn").click(function(){
	 $('#shareholderForm').reset(); 
	 $("#saveShareholderReset").click();
	 $('#saveShareholderMiss').click(); 
});

function addShareholderReset(){
	$("#saveShareholderReset").click();
	 $('#saveShareholderMiss').click(); 
}
//新增股东成员
$("#saveShareholder").click(function(){
	var shareholderForm = $('#shareholderForm');
	var flag = shareholderForm.valid();
    if(!flag){
    	return flag;
    }
  //新增股东
	var json = $('form#shareholderForm').serializeJSON();
	var js = json.replace('}', ',');
  	json = js + "\"enterpriseId\":\""+ $("#enterpriseId").val()+"\"}";
	$.ajax({  
	     type:"post",
         contentType:"application/json; charset=UTF-8",
         url: ctx + "/enterprise/addShareholder",
         data:json,
         dataType:"json",
	     success:function(data) {  
	    	 if(data.code == 1){
	    		 var flagfullTime = "";
	    		 var fullTime = $("#fullTime").val();
	    		 if( fullTime == "1"){
	    			 flagfullTime = "是";
	    		 }else if(fullTime == "0"){
	    			 flagfullTime = "否";
	    		 }
	    		 $("#Shareholdertable tr:eq(0)").after("<tr role=\"row\" class=\"odd\"><td> <center>"+$("#shareholder_name").val()+"</center></td>"+
	    					"<td>"+
	    						"<center>"+$("#shareholder_position").val()+"</center>"+
	    					"</td>"+
	    					"<td>"+
	    			           "<center>"+$("#totalShares").val()+"%<center>"+
	    			        "</td>"+
	    					"<td>"+
	    						"<center>"+flagfullTime+"</center>"+
	    					"</td>"+
	    					"<td>"+
	    			            "<center>"+$("#actualContributiveAmount").val()+"<center>"+
	    			            "</td>"+
	    			            "<td>"+
	    		            "<center>"+$("#sh_introduction").val()+"<center>"+
	    		            "</td>"+
	    					"<td>"+
	    						"<button type=\"button\"  id="+ data.data+" onclick=\"delete_this_row(this)\"; class=\"btn btn-primary\">删除</button>" +
	    					"</td>"+
	    			"</tr>");
	    			$("#saveShareholderMiss").click();
	    			$("#saveShareholderReset").click();
	    	 }
	    	 return false;
	      },  
	      error : function() {  
	    	  bootbox.alert("新增股东失败！");
	    	  return false;
	      }  
	 });
});

//删除股东成员
function delete_this_row(t){
	t.parentNode.parentNode.parentNode.removeChild(t.parentNode.parentNode);
	var json ="{\"id\":\""+t.id+"\"}";
	$.ajax({  
	     type:"post",
         contentType:"application/json; charset=UTF-8",
         url: ctx + "/enterprise/delShareholder",
         data:json,
         dataType:"json",
	     success:function(data) {  
	    	 if(data.code == 1){
	    		 return true;
	    	 }
	    	 return false;
	      },  
	      error : function() {  
	    	  bootbox.alert("删除股东成员失败！");
	    	  return false;
	      }  
	 });
	resetSequenceNum("Shareholdertable");
}

//重新设置序号  
function resetSequenceNum(str){  
    var num=0;
    $("#"+str+" tr").each(function(index,dom){  
        if(index!=0){  
            $(dom).find("td").first().html(arryList[num]);  
            num++;  
        }  
    });  
}

var enterpriseId;
//新增企业基本信息
function addEnterpriseBaseInfo(){
	if($("#enterpriseId").val() == ""){
		var json = $('form#addForm').serializeJSON();
		var content = $($('.note-editable')[0]).code();
        if("" == content.trim() || "<p><br></p>" == content.trim()){
         	return false;
         }
		var js = json.replace('}', ',');
    	json = js + "\"introduction\":\""+encodeURIComponent(content)+"\"}";
		$.ajax({  
		     type:"post",
	         contentType:"application/json; charset=UTF-8",
	         url: ctx + "/enterprise/doAdd",
	         data:json,
	         dataType:"json",
		     success:function(data) {  
		    	 if(data.code == 1){
		    		 $("#enterpriseId").val(data.data);
		    		 enterpriseId = data.data;
		    		 return true;
		    	 }
		    	 return false;
		      },  
		      error : function() {  
		    	  bootbox.alert("新增企业失败！");
		    	  return false;
		      }  
		 });
	}
}

//新增团队介绍信息
function addTeamIntroduce(){
	if($("#enterpriseId").val() != ""){
		var content = $($('.note-editable')[1]).code();
        if("" == content.trim() || "<p><br></p>" == content.trim()){
         	return false;
         }
		var json ="{\"id\":\""+$("#enterpriseId").val()+"\",\"coreMembersIntroduction\":\""+encodeURIComponent(content)+"\"}";
		$.ajax({  
		     type:"post",
	         contentType:"application/json; charset=UTF-8",
	         url: ctx + "/enterprise/update",
	         data:json,
	         dataType:"json",
		     success:function(data) {  
		    	 if(data.code == 1){
		    		 return true;
		    	 }
		    	 return false;
		      },  
		      error : function() {  
		    	  bootbox.alert("新增企业失败！");
		    	  return false;
		      }  
		 });
	}
}

//新增企业商业模式信息
function addBusinessModel(){
	if($("#enterpriseId").val() != ""){
		var content2 = $($('.note-editable')[2]).code();
		var content3 = $($('.note-editable')[3]).code();
		var content4 = $($('.note-editable')[4]).code();
		var content5 = $($('.note-editable')[5]).code();
		var content6 = $($('.note-editable')[6]).code();
		var content7 = $($('.note-editable')[7]).code();
        if("" == content2.trim() || "<p><br></p>" == content2.trim() || "" == content3.trim() || "<p><br></p>" == content3.trim()|| "" == content4.trim() || "<p><br></p>" == content4.trim() || "" == content5.trim() || "<p><br></p>" == content5.trim() || "" == content6.trim() || "<p><br></p>" == content6.trim() || "" == content7.trim() || "<p><br></p>" == content7.trim()){
         	return false;
         }
	 	var json ="{\"id\":\""+$("#enterpriseId").val()+"\",\"mainBusiness\":\""+encodeURIComponent(content2)+"\",\"IndustrySituation\":\""+encodeURIComponent(content3)+"\",\"competitivePosition\":\""+encodeURIComponent(content4)+"\",\"mainAssetConditions\":\""+encodeURIComponent(content5)+"\",\"businessModel\":\""+encodeURIComponent(content6)+"\",\"businessOther\":\""+encodeURIComponent(content7)+"\"}";
		$.ajax({  
		     type:"post",
	         contentType:"application/json; charset=UTF-8",
	         url: ctx + "/enterprise/update",
	         data:json,
	         dataType:"json",
		     success:function(data) {  
		    	 if(data.code == 1){
		    		 return true;
		    	 }
		    	 return false;
		      },  
		      error : function() {  
		    	  bootbox.alert("新增企业失败！");
		    	  return false;
		      }  
		 });
	}
}

//新增财务数据信息
function addFinancialData(){
	if($("#enterpriseId").val() != ""){
		var content8 = $($('.note-editable')[8]).code();
		var content9 = $($('.note-editable')[9]).code();
        if("" == content8.trim() || "<p><br></p>" == content8.trim() || "" == content9.trim() || "<p><br></p>" == content9.trim()){
         	return false;
         }
		var json ="{\"id\":\""+$("#enterpriseId").val()+"\",\"profitForecast\":\""+encodeURIComponent(content8)+"\",\"financiaOther\":\""+encodeURIComponent(content9)+"\"}";
		$.ajax({  
		     type:"post",
	         contentType:"application/json; charset=UTF-8",
	         url: ctx + "/enterprise/update",
	         data:json,
	         dataType:"json",
		     success:function(data) {  
		    	 if(data.code == 1){
		    		 $("#enterpriseId").val(data.data);
		    		 return true;
		    	 }
		    	 return false;
		      },  
		      error : function() {  
		    	  bootbox.alert("新增财务数据失败！");
		    	  return false;
		      }  
		 });
	}
}

//新增企业附件
  function uploadRelatedFile(){
		if(!$("#relatedFile").val()){
			bootbox.alert("请先添加文件！");
			return;
		}
		 $.ajaxFileUpload({
                url: ctx + '/enterpriseAttachment/uploadRelatedFile',
                type: 'post',
                secureuri: false,
                fileElementId: 'relatedFile',
                dataType: 'json',
                data: {'enterpriseId': enterpriseId},
                success: function(data, status){
                	if (data.code == 1) {
                		bootbox.dialog({
        					message : "上传成功！ ",
        					title : "温馨提示：",
        					buttons : {
        						main : {
        							label : "确定",
        							className : "btn btn-outline btn-primary",
        							callback : function() {
        								getRelatedFile();
        							}
        						}
        					}
        				});
                		
                	} else {
                		bootbox.alert(data.message);
                	}
                },
                error: function(msg, status, e){
                	bootbox.alert("上传失败！");
                }
            });
		 } 
    
    
    function deleteFile(obj){
        bootbox.confirm("您确定要删除这个文件?", function(result) {
            if (result) {
            	var nRow = $(obj).parents('div')[0];
                var id = nRow.id;
                
                $.ajax({
                	type:"get",
                    url: ctx + "/enterpriseAttachment/delete",
                    data:"id=" + id,
                    dataType:"json",
                    success: function(msg){
                    	if (msg.code == 1) {
                    		getRelatedFile();
                    		bootbox.alert("删除文件成功！");
                    	} else {
                    		bootbox.alert("删除文件失败！");
                    	}
                    },
                    error: function (msg) {
                    	bootbox.alert(msg.message);
                    }
                });
            }
        });
    }
    
    function getRelatedFile(){
    	$.ajax({
        	type:"get",
            url: ctx + "/enterpriseAttachment/query/"+enterpriseId,
            dataType:"json",
            success: function(data){
            	if (data.code == 1) {
            		var objs = data.data.relatedFile;
                	var html = "";
                	var closeBttn = '<button onclick="deleteFile(this)" style="border:0px;background:#0f0;background-color:transparent;" title="Close">'+
						            '<span aria-hidden="true">&times;</span>'+
							        '<span class="hide">Close</span>'+
					                '</button>';
					
                	for(var i=0;i<objs.length;i++){
                		var id = objs[i].id;
                		var fileName = objs[i].fileName;
                		var renameBttn = '<button id="'+id+'" name="'+fileName+'" type="button" class="btn btn-outline btn-primary btn-xs" onclick="showRenameDialog(this)">重命名</button>';
                		html+="<div style='padding-top:2px' id='"+id+"'>"+fileName+closeBttn+renameBttn+"</div>";
                	}
                	$("#relatedFileId").html(html);
            	}
            },
            error: function (msg) {
            	bootbox.alert(msg.message);
            }
        });
    }
    var modal;
    function showRenameDialog(obj){
    	var filename = $(obj).attr('name').split(".")[0];
    	$("#o_renameFileName").val(filename);
    	$("#renameFileName").val(filename);
    	$("#renameFileName").attr('name',$(obj).attr('id'));
    	modal = $('#updateModel_relatedFile').modal('show');
    }
    
    function renameFile(){
    	var o_filename = $("#o_renameFileName").val();
    	var filename = $("#renameFileName").val();
    	if(o_filename==filename){
    		bootbox.alert("请输入与原文件名不同的名字！");
    		return;
    	}
    	
        var id = $("#renameFileName").attr('name');

    	$.ajax({
        	type:"post",
            url: ctx + "/enterpriseAttachment/rename",
            data:"id=" + id+"&fileName="+filename,
            dataType:"json",
            success: function(data){
            	if (data.code == 1) {
            		modal.modal('hide');
            		bootbox.alert("更新文件名字成功！");
            		getRelatedFile();
            	}
            },
            error: function (msg) {
            	bootbox.alert(msg.message);
            }
        });
    }
 
